﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using SharpDiary.Core.Common;
using SharpDiary.Core.Attributes;
using SharpDiary.Core.ORMapping;
using SharpDiary.Core.Data;
using SharpDiary.Core.Utility;

namespace SharpDiary.Task
{
    [GuiModule("3cbe438e-37a3-428a-aae3-a15acc5d47a8", "TaskManager", "Ray Shaw(demogodyou@gmail.com)")]
    public partial class frmTaskManager : GuiModuleUIBase
    {
        #region Fields & Properties


        #endregion

        #region Constructors

        public frmTaskManager()
        {
            InitializeComponent();
        }

        #endregion

        #region Methods

        public override string GetSettingsKeyName()
        {
            return this.Name;
        }

        protected override void OnShown(EventArgs e)
        {
            base.OnShown(e);

            taskListControl.ShowItems();
            BuildTaskStatusButtons();
            UpdateUI();
        }

        private void UpdateUI()
        {
            toolStripStatusLabelMain.Text = string.Format("共 {0} 个任务", TaskInfo.GetTotalTaskCount());
        }

        /// <summary>
        /// 查询。
        /// </summary>
        private void Search()
        {
            StringBuilder sql = new StringBuilder();
            sql.AppendLine("select * from TaskInfo");
            sql.AppendLine("where (Name like @keyword");
            sql.AppendLine("or Description like @keyword)");

            DataAccessService das = Utils.GetDataAccessServiceInstance();
            taskListControl.ShowItems(sql.ToString(), das.CreateParameter("@keyword", DbType.String,
                string.Format("%{0}%", toolStripTextBoxKeyword.Text)));
        }

        /// <summary>
        /// 创建所有任务状态按钮。
        /// </summary>
        private void BuildTaskStatusButtons()
        {
            List<TaskStatusInfo> allStatus = TaskStatusInfo.SelectAll<TaskStatusInfo>();
            foreach (TaskStatusInfo task in allStatus)
            {
                ToolStripButtonEx btn = new ToolStripButtonEx();
                btn.Text = task.Name;
                btn.Image = Utils.BytesToImage(task.Picture);
                btn.ToolTipText = string.Format("只显示{0}的任务。", task.Name);
                btn.Sql = "select * from TaskInfo where Status = " + task.Id.ToString();
                btn.CheckOnClick = true;
                btn.Click += new EventHandler(btn_Click);

                toolStripMain.Items.Add(btn);
            }
        }

        private void btn_Click(object sender, EventArgs e)
        {
            ToolStripButtonEx button = sender as ToolStripButtonEx;
            if (button == null) { return; }

            MakeOnlyOneButtonCheck(button);

            string sql = button.Sql;
            if (string.IsNullOrEmpty(sql)) { return; }

            taskListControl.ShowItems(sql);
        }

        private void MakeOnlyOneButtonCheck(ToolStripButton button)
        {
            //如果传递 null, 则将全部筛选按钮置为unchecked.
            if (button == null)
            {
                foreach (ToolStripItem item in toolStripMain.Items)
                {
                    if (item is ToolStripButtonEx)
                    {
                        (item as ToolStripButtonEx).Checked = false;
                    }
                }
            }
            else
            {
                button.Checked = true;

                foreach (ToolStripItem item in toolStripMain.Items)
                {
                    if (item != button && item is ToolStripButtonEx)
                    {
                        (item as ToolStripButtonEx).Checked = false;
                    }
                }
            }
        }

        public override void OnMessageReceived(object sender, MessageReceivedEventArgs e)
        {
            base.OnMessageReceived(sender, e);

            if (e.TargetModuleId.Equals("3cbe438e-37a3-428a-aae3-a15acc5d47a8", StringComparison.CurrentCultureIgnoreCase))
            {
                switch (e.MessageInfo)
                {
                    case MessageDefinition.Refresh: //刷新
                        {
                            RefreshData();
                            break;
                        }
                }
            }
        }

        /// <summary>
        /// 刷新显示。
        /// </summary>
        private void RefreshData()
        {
            taskListControl.ShowItems();
            UpdateUI();
        }

        #endregion

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            RefreshData();
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            OwnerShell.ExecuteAction("SharpDiary.Task", "21aee42e-c54f-4201-864a-605390dd78fd", null, this);
        }

        private void toolStripButtonNew_Click(object sender, EventArgs e)
        {
            OwnerShell.ExecuteAction("SharpDiary.Task", "21aee42e-c54f-4201-864a-605390dd78fd", null, this);
        }

        private void toolStripButtonRefresh_Click(object sender, EventArgs e)
        {
            MakeOnlyOneButtonCheck(null);

            toolStripButtonRefresh.Enabled = false;
            Application.DoEvents();

            try
            {
                taskListControl.ShowItems();
                UpdateUI();
            }
            finally
            {
                toolStripButtonRefresh.Enabled = true;
            }
        }

        private void toolStripButtonSearch_Click(object sender, EventArgs e)
        {
            toolStripButtonSearch.Enabled = false;
            Application.DoEvents();
            try
            {
                Search();
            }
            finally
            {
                toolStripButtonSearch.Enabled = true;
            }
        }

        private void toolStripTextBoxKeyword_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                toolStripButtonSearch.PerformClick();
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            Close();
        }
    }
}
